package com.xingkeduo.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.boot.autoconfigure.SpringBootVFS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;
import java.sql.SQLException;

@Configuration
@Conditional(MybatisPlusCondition.class)
public class MybatisPlusSqlSessionConfig {
	@Value("${db.mysql.message.driver}")
	private String driver;
	@Value("${db.mysql.message.url}")
	private String url;
	@Value("${db.mysql.message.user}")
	private String user;
	@Value("${db.mysql.message.password}")
	private String password;
	@Value("${db.mysql.message.maxActive}")
	private int maxActive;
	@Value("${db.mysql.message.initialSize}")
	private int initialSize;
	@Value("${db.mysql.message.maxWait}")
	private long maxWait;
	@Value("${db.mysql.message.minIdle}")
	private int minIdle;

	@Bean
	public DataSource initDataSource() throws SQLException {
		DruidDataSource dataSource = new DruidDataSource();
		//TODO chi
		dataSource.setDriverClassName(driver);
		dataSource.setUrl(url);
		dataSource.setUsername(user);
		dataSource.setPassword(password);

		return dataSource;
	}


	@Bean(name = "mysqlUserTransactionManager")
	public DataSourceTransactionManager initTransactionManager(@Autowired DataSource dataSource) {
		try {
			return new DataSourceTransactionManager(dataSource);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}



	@Bean(name = "mysqlUserSqlSessionFactory")
	public SqlSessionFactory initSqlSessionFactory(@Autowired DataSource dataSource
			,@Autowired GlobalConfig mpGlobalConfig
			,@Autowired PaginationInterceptor paginationInterceptor)
			throws Exception {

		MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
		mybatisSqlSessionFactoryBean.setDataSource(dataSource);
		//enum映射
		mybatisSqlSessionFactoryBean.setTypeEnumsPackage("com.xingkeduo.pojo.enums");

		MybatisConfiguration mybatisConfiguration = new MybatisConfiguration();
		mybatisConfiguration.setLogImpl(org.apache.ibatis.logging.stdout.StdOutImpl.class);
		mybatisConfiguration.setMapUnderscoreToCamelCase(true);
		mybatisSqlSessionFactoryBean.setConfiguration(mybatisConfiguration);


		mybatisSqlSessionFactoryBean.setGlobalConfig(mpGlobalConfig);

		mybatisSqlSessionFactoryBean.setPlugins(new Interceptor[]{paginationInterceptor});

		ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
		mybatisSqlSessionFactoryBean.setTypeAliasesPackage("com.xingkeduo.pojo.table,com.xingkeduo.pojo.query");
		mybatisSqlSessionFactoryBean.setVfs(SpringBootVFS.class);
		//mybatisSqlSessionFactoryBean.setConfigLocation(resolver.getResource("classpath:mybatis.xml"));

		return mybatisSqlSessionFactoryBean.getObject();
	}

	@Bean(name = "mysqlUserMasterSqlSessionTemplateFromMybatis")
	public SqlSessionTemplate initSqlSessionTemplate(
			@Qualifier("mysqlUserSqlSessionFactory") SqlSessionFactory sqlSessionFactory){
		return new SqlSessionTemplate(sqlSessionFactory);
	}

/*	@Bean(name = "mysqlSystemDao")
	public DatabaseAccessObject<T, Q> initMysqlSystemDao(
			@Qualifier("mysqlSystemSqlSessionTemplateFromMybatis") SqlSessionTemplate sqlSessionTemplate) {
		return new DatabaseProxy<T, Q>().build(sqlSessionTemplate,null);
	}*/
}
